package com.hw.blog.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.hw.blog.common.PaginationInfo;
import com.hw.blog.service.PmgrBoardService;
import com.hw.blog.utils.ToJinsu;


/**
 * 관리자 FAQ 관리
 * @author oracle
 *
 */
@Controller
public class PmgrBoardController extends BaseController{
	@Autowired
	private PmgrBoardService service;

	private Logger log = Logger.getLogger(getClass());
	
	/**
	 * 메뉴 목록 조회
	 * @param req
	 * @param res
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/pmgr/brd/brdList")
	public ModelAndView brdList(@RequestParam Map<String,Object> vo,HttpServletRequest req, HttpServletResponse res) throws Exception {
		ModelAndView mav = new ModelAndView();
		// 페이징 시작
		PaginationInfo page = new PaginationInfo();
		int currnetPageNo =1;
		if(null != vo.get("currentPageNo") && !"".equals(vo.get("currentPageNo"))){
			currnetPageNo = Integer.parseInt( vo.get("currentPageNo").toString());
		}
		if(null != vo.get("recordCountPerPage") && !"".equals(vo.get("recordCountPerPage"))){
			page.setRecordCountPerPage(Integer.parseInt( vo.get("recordCountPerPage").toString()));
		}
		
		page.setCurrentPageNo(currnetPageNo);
		page.setParamMap(vo);

		int iTotalCnt = service.brdListCnt(vo);
		page.setTotalRecordCount(iTotalCnt);
		
		if(iTotalCnt>0){
			vo.put("_page", page);
			mav.addObject("list",service.brdList(vo) );	
		}
		
		mav.addObject("_page", page);
		mav.addObject("paging", page.getAdmHtml("brdList.do"));
		
		
		return mav;
	}
	
	/**
	 * 수정하기
	 * @param vo
	 * @param req
	 * @param res
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/pmgr/brd/brdEdit", method = RequestMethod.GET)
	public ModelAndView brdEdit(@RequestParam Map<String,Object> vo,HttpServletRequest req, HttpServletResponse res) throws Exception {
		ModelAndView mav = new ModelAndView();

		if(null != vo.get("bd_no") && !"".equals(vo.get("bd_no") )){
			mav.addObject("map",service.brdEdit(vo) );
			mav.addObject("files",service.brdFileEdit(vo) );
		}
		PaginationInfo page = new PaginationInfo();
		vo.remove("bd_no");
		page.setParamMap(vo);
		
		mav.addObject("_page", page);
		
		return mav;
	}

	/**
	 * 수정및 등록 프로세스
	 * @param vo
	 * @param req
	 * @param res
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/pmgr/brd/editProcess")
	public ModelAndView editProcess(@RequestParam Map<String,Object> vo,
			@RequestParam(value="bdf_no", required=false) String[] bdf_no,
			@RequestParam(value="upload", required=false) String[] upload,
			@RequestParam(value="upload_nm", required=false) String[] upload_nm,
			@RequestParam(value="upload_sz", required=false) String[] upload_sz,
			@RequestParam(value="file_del_yn", required=false) String[] file_del_yn,
			HttpServletRequest req, HttpServletResponse res) throws Exception {
		if(bdf_no!=null){
			String[] delYns = new String[bdf_no.length];
			for(int i =0;i < delYns.length;i++){
				delYns[i]="";
			}
			if(file_del_yn!=null){
				for(int i =0;i < file_del_yn.length;i++){
					delYns[Integer.parseInt(file_del_yn[i])]="Y";
				}	
			}
			
			
			vo.put("bdf_no", bdf_no);
			vo.put("upload", upload);
			vo.put("upload_nm", upload_nm);
			vo.put("upload_sz", upload_sz);
			vo.put("del_yns", delYns);
			vo.put("was_path", req.getSession().getServletContext().getRealPath(""));
		}
		
		ModelAndView mav = new ModelAndView("redirect:./brdList.do?menu_seq="+vo.get("menu_seq"));

		//있으면 수정 없으면 업데이트
		service.updateBrd(vo);

		return mav;
	}
	
	
	/**
	 * 삭제 프로세스
	 * @param vo
	 * @param req
	 * @param res
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/pmgr/brd/deleteProcess")
	public ModelAndView deleteProcess(@RequestParam Map<String,Object> vo,HttpServletRequest req, HttpServletResponse res) throws Exception {
		ModelAndView mav = new ModelAndView("redirect:./faqList.do");

		mav.addObject("menuList",service.deleteProcess(vo) );	

		return mav;
	}

	
	/**
	 * 파일 추가
	 * @param vo
	 * @param req
	 * @param res
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/pmgr/brd/addFile")
	public ModelAndView addFile(@RequestParam Map<String,Object> vo,HttpServletRequest req, HttpServletResponse res) throws Exception {
		ModelAndView mav = new ModelAndView("redirect:./faqList.do");

		mav.addObject("menuList",service.deleteProcess(vo) );	

		return mav;
	}
	
	
	/**
	 * 댓글 목록 가져오기
	 * @param vo
	 * @param req
	 * @param res
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/pmgr/brd/cmtList")
	public String cmtList(@RequestParam Map<String,Object> vo,HttpServletRequest req, HttpServletResponse res) throws Exception {
		ModelAndView mav = new ModelAndView("redirect:./faqList.do");

		mav.addObject("menuList",service.deleteProcess(vo) );	

		return "jsonView";
	}
	
	/**
	 * 댓글 수정하기
	 * @param vo
	 * @param req
	 * @param res
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/pmgr/brd/edtCmt")
	public ModelAndView edtCmt(@RequestParam Map<String,Object> vo,HttpServletRequest req, HttpServletResponse res) throws Exception {
		ModelAndView mav = new ModelAndView("redirect:./faqList.do");

		mav.addObject("menuList",service.deleteProcess(vo) );	

		return mav;
	}
	
	
	/**
	 * 댓글 수정 및 등록 프로세스
	 * @param vo
	 * @param req
	 * @param res
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/pmgr/brd/edtCmtProcess")
	public String edtCmtProcess(@RequestParam Map<String,Object> vo,HttpServletRequest req, HttpServletResponse res) throws Exception {
		ModelAndView mav = new ModelAndView("redirect:./faqList.do");

		
		
		if(vo.get("bd_cmt_no")==null || "".equals(vo.get("bd_cmt_no"))){
			//신규
			
			Map map = service.getTopCmt(vo);
			
			if(map==null){
				vo.put("bd_ord", "zz");	
			}else{
				String bd_ord = (String)map.get("bd_ord");
				String num = bd_ord.substring( bd_ord.length()-2,bd_ord.length());
				
				int iNum = Integer.parseInt( ToJinsu.jinsuToDec(num));
				iNum--;
				
				if(iNum<0){
					//더이상 댓글을 쓸수 없습니다.
					
				}else{
					vo.put("bd_ord",bd_ord.substring( 0,bd_ord.length()-2)+ ToJinsu.getJinsu(Integer.toString(iNum) ));
				}
			}
			service.insertCmt(vo);
			
		}else{
			//수정
//			service.updateCmt(vo);
		}
		
		
		return "jsonView";
	}
	
	/**
	 * 댓글 삭제 프로세스
	 * @param vo
	 * @param req
	 * @param res
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/pmgr/brd/delCmtProcess")
	public String delCmtProcess(@RequestParam Map<String,Object> vo,HttpServletRequest req, HttpServletResponse res) throws Exception {
		ModelAndView mav = new ModelAndView("redirect:./faqList.do");

		mav.addObject("menuList",service.deleteProcess(vo) );	

		return "jsonView";
	}
	
}
